﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcJE.Core.Inet;

namespace MvcJE.Web.Areas.Inet.Controllers
{
    public class MopInputDataController : Controller
    {
        private IPServiceDb db = new IPServiceDb();

        //
        // GET: /Inet/MopInputData/

        public ViewResult Index(Guid? id,int start = 0, int itemsPerPage = 20, string orderBy = "DateCreated", bool desc = false)
        {
              ViewBag.Count = db.MopInputData.Count();          
            if (id != null)
            {
                var gid=(Guid) id;
               var data=db.MopInputData.Where(p=>p.MopModuleId==gid);
               ViewBag.Count = data.Count();
            }

            ViewBag.Start = start;
            ViewBag.ItemsPerPage = itemsPerPage;
            ViewBag.OrderBy = orderBy;
            ViewBag.Desc = desc;
            ViewBag.Id = id;
            return View();
        }

        //
        // GET: /Inet/MopInputData/GridData/?start=0&itemsPerPage=20&orderBy=MopInputDataId&desc=true

        public ActionResult GridData(Guid? id, int start = 0, int itemsPerPage = 20, string orderBy = "DateCreated", bool desc = false)
        {
            if (id != null)
            {

                Response.AppendHeader("X-Total-Row-Count", db.MopInputData.Where(p => p.MopModuleId == id).Count().ToString());
                ObjectQuery<MopInputData> mopinputdata = (db as IObjectContextAdapter).ObjectContext.CreateObjectSet<MopInputData>();
                mopinputdata = mopinputdata.OrderBy("it." + orderBy + (desc ? " desc" : ""));

                return PartialView(mopinputdata.Where(p => p.MopModuleId == id).Skip(start).Take(itemsPerPage));
            }
            Response.AppendHeader("X-Total-Row-Count", db.MopInputData.Count().ToString());
            ObjectQuery<MopInputData> mopinputdata2 = (db as IObjectContextAdapter).ObjectContext.CreateObjectSet<MopInputData>();
            mopinputdata2 = mopinputdata2.OrderBy("it." + orderBy + (desc ? " desc" : ""));

            return PartialView(mopinputdata2.Skip(start).Take(itemsPerPage));
        }

        //
        // GET: /Default5/RowData/5

        public ActionResult RowData(Guid id)
        {
            MopInputData mopinputdata = db.MopInputData.Find(id);
            return PartialView("GridData", new MopInputData[] { mopinputdata });
        }

        //
        // GET: /Inet/MopInputData/Create

        public ActionResult Create()
        {
            return PartialView("Edit");
        }

        //
        // POST: /Inet/MopInputData/Create

        [HttpPost]
        public ActionResult Create(MopInputData mopinputdata)
        {
            if (ModelState.IsValid)
            {
                mopinputdata.MopInputDataId = Guid.NewGuid();
                db.MopInputData.Add(mopinputdata);
                db.SaveChanges();
                return PartialView("GridData", new MopInputData[] { mopinputdata });
            }

            return PartialView("Edit", mopinputdata);
        }

        //
        // GET: /Inet/MopInputData/Edit/5

        public ActionResult Edit(Guid id)
        {
            MopInputData mopinputdata = db.MopInputData.Find(id);
            return PartialView(mopinputdata);
        }

        //
        // POST: /Inet/MopInputData/Edit/5

        [HttpPost]
        public ActionResult Edit(MopInputData mopinputdata)
        {
            if (ModelState.IsValid)
            {
                db.Entry(mopinputdata).State = EntityState.Modified;
                db.SaveChanges();
                return PartialView("GridData", new MopInputData[] { mopinputdata });
            }

            return PartialView(mopinputdata);
        }

        //
        // POST: /Inet/MopInputData/Delete/5

        [HttpPost]
        public void Delete(Guid id)
        {
            MopInputData mopinputdata = db.MopInputData.Find(id);
            db.MopInputData.Remove(mopinputdata);
            db.SaveChanges();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
